namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganLevelBLL
    {
        public int Add(OrganLevel model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganLevel(");
            builder.Append("LevelName)");
            builder.Append(" values (");
            builder.Append("@LevelName)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelName", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.LevelName;
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int LevelID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganLevel ");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LevelID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public List<OrganLevel> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select LevelID,LevelName ");
            builder.Append(" FROM OrganLevel ");
            DataSet set = DBHelper.Query(builder.ToString());
            List<OrganLevel> list = null;
            OrganLevel item = null;
            if (set.Tables[0].Rows.Count > 0)
            {
                list = new List<OrganLevel>();
                foreach (DataRow row in set.Tables[0].Rows)
                {
                    item = new OrganLevel();
                    if ((row["LevelID"] != null) && (row["LevelID"].ToString() != ""))
                    {
                        item.LevelID = int.Parse(row["LevelID"].ToString());
                    }
                    if ((row["LevelName"] != null) && (row["LevelName"].ToString() != ""))
                    {
                        item.LevelName = row["LevelName"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public OrganLevel GetModel(int LevelID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 LevelID,LevelName from OrganLevel ");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = LevelID;
            OrganLevel level = new OrganLevel();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if ((row["LevelID"] != null) && (row["LevelID"].ToString() != ""))
                {
                    level.LevelID = int.Parse(row["LevelID"].ToString());
                }
                if ((row["LevelName"] != null) && (row["LevelName"].ToString() != ""))
                {
                    level.LevelName = row["LevelName"].ToString();
                }
                return level;
            }
            return null;
        }

        public bool Update(OrganLevel model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganLevel set ");
            builder.Append("LevelName=@LevelName");
            builder.Append(" where LevelID=@LevelID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LevelName", SqlDbType.NVarChar, 50), new SqlParameter("@LevelID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.LevelName;
            cmdParms[1].Value = model.LevelID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

